package msg;

import java.text.MessageFormat;
import java.util.*;

import user.User;

import common.interfaces.IMessageVisitor;

/**
 * A message type with a capacity and a time
 * 
 * @author Gal Lalouche
 */
public class EventMessage extends Message {
	private final int	capacity;
	
	/**
	 * Creates a new EventMessage
	 * 
	 * @param messageId The message's unique id
	 * @param location The event's location
	 * @param content The event's content
	 * @param poster The poster of the event
	 * @param eventTime The event's time
	 * @param capacity The event's maximum capacity
	 * @throws ArithmeticException If {@code capacity <= 0}
	 */
	EventMessage(long messageId, Location location, String content, User poster, Calendar eventTime, int capacity) {
		this(messageId, location, content, poster, eventTime.getTimeInMillis(), capacity);
	}
	
	/**
	 * Creates a new EventMessage
	 * 
	 * @param messageId The message's unique id
	 * @param location The event's location
	 * @param content The event's content
	 * @param poster The poster of the event
	 * @param eventTime The event's time in unix time
	 * @param capacity The event's maximum capacity
	 * @throws ArithmeticException If {@code capacity <= 0}
	 */
	EventMessage(long messageId, Location location, String content, User poster, long eventTime, int capacity) {
		super(messageId, location, content, poster, eventTime);
		
		if (capacity <= 0) {
			throw new ArithmeticException("capacity must be positive; is: " + capacity);
		}
		this.capacity = capacity;
	}
	
	/**
	 * @return the capacity
	 */
	public int getCapacity() {
		return capacity;
	}
	
	@Override
	public boolean equals(Object obj) {
		return (obj instanceof EventMessage) && super.equals(obj);
	}
	
	@Override
	public <T> T accept(IMessageVisitor<T> visitor) throws Exception {
		return visitor.visit(this);
	}
	
	/**
	 * @return <code>true</code> iff the event is over
	 */
	public boolean isOver() {
		return getTime() < new Date().getTime();
	}
	
	@Override
	public String toString() {
		return MessageFormat.format("Event({0})", getId());
	}
}
